package cn.com.fstar.framework.admin.controller;

import cn.com.fstar.core.CodeConstant;
import cn.com.fstar.core.pageModel.Dictionary;
import cn.com.fstar.core.pageModel.Role;
import cn.com.fstar.core.pageModel.Tree;
import cn.com.fstar.core.util.LogUtil;
import cn.com.fstar.core.util.StringUtil;
import cn.com.fstar.framework.admin.service.DictionaryService;
import cn.com.fstar.framework.base.controller.BaseController;
import cn.com.fstar.framework.base.model.EasyUIComboTree;
import cn.com.fstar.framework.base.model.JsonResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 字典信息管理
 * 
 * @author 
 * 
 */
@Controller
@RequestMapping("/admin/dictionary")
public class DictionaryController extends BaseController {

    private static final Logger LOG = Logger.getLogger(DictionaryController.class);

    @Autowired
    private DictionaryService dictionaryService = null;

    /**
     * 跳转到字典管理页面
     * 
     * @author Guodong 2015-06-11 15:55:57
     * @return
     */
    @RequestMapping("/manager")
    public String dictionary(HttpServletRequest request) {
        return "/admin/dictionary/dictionaryList";
    }

    /**
     * 获取字典list
     * 
     * @author Guodong 2015-06-11 15:56:03
     * @param
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/dictionaryDataGrid")
    public List<Dictionary> dictionaryDataGrid(Dictionary dictionary, HttpServletRequest request, HttpSession session) {
        return dictionaryService.dictionaryDataGrid(dictionary);
    }

    /**
     * 跳转到角色添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        Role r = new Role();
        r.setId(StringUtil.generateUUID());
        request.setAttribute("role", r);
        return "/admin/dictionary/dictionaryAdd";
    }

    /**
     * 添加字典
     * 
     * @author Guodong 2015-06-11 19:20:21
     * @return
     */
    @ResponseBody
    @RequestMapping("/addDictionary")
    public JsonResult addDictionary(Dictionary dictionary, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        if (dictionary != null) {
            // 调用Service方法
            try {
                dictionary.setId(StringUtil.generateUUID());
                String msg = dictionaryService.add(dictionary);
                if (msg == "success") {
                    j.setObj("success");
                    j.setMsg("新数据字典添加成功！");
                    LogUtil.adminLogs("新数据字典添加成功：", CodeConstant.ADD_OPERATION, request);
                    // 记录后台日志
                    // LogUtil.logSys("新增ID为【" + dictionary.getId() + "】，名称【" +
                    // dictionary.getDictName() + "】的数据字典",
                    // Constant.OPER_ADD, request);
                    // LogUtil.logBusiness("新增ID为【" + dictionary.getId() +
                    // "】，名称【" +
                    // dictionary.getDictName() + "】的数据字典",
                    // Constant.OPER_ADD, request);
                } else if (msg == "zdCodeExist") {
                    j.setObj("zdCodeExist");
                    j.setMsg("【字典编号】已经存在，请重新输入！");
                    LogUtil.adminLogs("新数据字典添加失败：", CodeConstant.ADD_OPERATION, request);
                } else if (msg == "zdNameExist") {
                    j.setObj("zdNameExist");
                    j.setMsg("【字典名称】已经存在，请重新输入！");
                    LogUtil.adminLogs("新数据字典添加失败：", CodeConstant.ADD_OPERATION, request);
                } else {
                    j.setObj("error");
                    j.setMsg("未知错误，请联系系统维护人员！");
                    LogUtil.adminLogs("新数据字典添加失败：", CodeConstant.ADD_OPERATION, request);
                }
            } catch (Exception e) {
                j.setObj("excep");
                j.setMsg("操作异常，请联系系统维护人员！");
                LogUtil.adminLogs("新数据字典添加失败:", CodeConstant.ADD_OPERATION, request);
                e.printStackTrace();
            }
        } else {
            j.setObj("objNull");
            j.setMsg("保存对象为空，请重新操作！");
            LogUtil.adminLogs("新数据字典添加失败:", CodeConstant.ADD_OPERATION, request);
        }

        return j;
    }

    /**
     * 跳转到字典修改页面
     * 
     * @author Guodong 2015-06-11 19:21:21
     * @return
     */
    @RequestMapping("/editDictionaryPage")
    public String editPage(HttpServletRequest request, String id, String parentId) {
        Dictionary d = dictionaryService.getDictionary(id, parentId);
        request.setAttribute("dictionary", d);
        return "/admin/dictionary/dictionaryEdit";
    }

    /**
     * 修改字典
     */
    @ResponseBody
    @RequestMapping("/editDictionary")
    public JsonResult editDictionary(Dictionary dictionary, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        if (dictionary != null) {
            // 调用Service方法
            try {
                String msg = dictionaryService.edit(dictionary);
                if (msg == "success") {
                    j.setObj("success");
                    j.setMsg("数据字典修改成功！");
                    LogUtil.adminLogs("新数据字典修改成功：", CodeConstant.EDIT_OPERATION, request);
                    // 记录后台日志
                    // LogUtil.logSys("ID为【" + dictionary.getId() +
                    // "】的数据字典已被修改",
                    // Constant.OPER_EDIT, request);
                    // LogUtil.logBusiness("ID为【" + dictionary.getId() +
                    // "】的数据字典已被修改",
                    // Constant.OPER_EDIT, request);
                } else if (msg == "zdCodeExist") {
                    j.setObj("zdCodeExist");
                    j.setMsg("【字典编号】已经存在，请重新输入！");
                    LogUtil.adminLogs("新数据字典修改失败：", CodeConstant.EDIT_OPERATION, request);
                } else if (msg == "zdNameExist") {
                    j.setObj("zdNameExist");
                    j.setMsg("【字典名称】已经存在，请重新输入！");
                    LogUtil.adminLogs("新数据字典修改失败：", CodeConstant.EDIT_OPERATION, request);
                } else {
                    j.setObj("error");
                    j.setMsg("未知错误，请联系系统维护人员！");
                    LogUtil.adminLogs("新数据字典修改失败：", CodeConstant.EDIT_OPERATION, request);
                }
            } catch (Exception e) {
                j.setObj("excep");
                j.setMsg("操作异常，请联系系统维护人员！");
                LogUtil.adminLogs("新数据字典修改失败", CodeConstant.EDIT_OPERATION, request);
                e.printStackTrace();
            }
        } else {
            j.setObj("objNull");
            j.setMsg("保存对象为空，请重新操作！");
            LogUtil.adminLogs("新数据字典修改失败", CodeConstant.EDIT_OPERATION, request);
        }

        return j;
    }

    /**
     * 获取数据字典列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/dictionaryComboTree")
    public List<EasyUIComboTree> dictionaryComboTree() {
        return dictionaryService.dictionarysComboTree();
    }

    /**
     * 获取数据字典列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/libComboTree")
    public List<EasyUIComboTree> libComboTree() {
        return dictionaryService.libComboTree();
    }

    /**
     * 获取数据字典列表 获取图书馆代码
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/libCodeComboTree")
    public List<EasyUIComboTree> libCodeComboTree() {
        return dictionaryService.libCodeComboTree();
    }

    /**
     * 获取网络连接状态
     */
    @ResponseBody
    @RequestMapping("/stateCodeComboTree")
    public List<EasyUIComboTree> stateCodeComboTree() {
        return dictionaryService.stateCodeComboTree();
    }

    /**
     * 删除字典
     * 
     * @author Guodong 2015-02-11
     * @param parentId
     *            id 角色对象ID
     * @return JsonResult 消息对象
     */
    @RequestMapping("/deleteDictionary")
    @ResponseBody
    public JsonResult delete(String id, String parentId, HttpSession session, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        // 调用Service方法
        try {
            String msg = dictionaryService.deleteDictionary(id, parentId);
            if (msg == "success") {
                j.setObj("success");
                j.setMsg("删除字典成功！");
                LogUtil.adminLogs("新数据字典删除成功", CodeConstant.DELETE_OPERATION, request);

            } else {
                j.setObj("error");
                j.setMsg("未知错误，请联系系统维护人员！");
                LogUtil.adminLogs("删除成功", CodeConstant.DELETE_OPERATION, request);
            }
        } catch (Exception e) {
            j.setObj("excep");
            j.setMsg("操作异常，请联系系统维护人员！");
            LogUtil.adminLogs("新数据字典删除失败", CodeConstant.DELETE_OPERATION, request);
            e.printStackTrace();
        }

        return j;
    }

    /**
     * 根据字典类型代码获取相应的字典树信息(下拉选择的时候使用)
     * 
     * @author lihf
     * @date 2016年2月19日 上午10:12:26
     * @param request
     * @return
     */
    @RequestMapping("/getDictTreeByTypeCode")
    @ResponseBody
    public List<Tree> getDictTreeByTypeCode(HttpServletRequest request) {
        List<Tree> tree = null;
        try {
            String dictCode = request.getParameter("dictCode");
            tree = dictionaryService.getDictTreeListByDictCode(dictCode);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return tree;
    }
}
